package com.ssau.expert.gui.common;

import com.ssau.expert.entity.Ship;
import com.ssau.expert.gui.app.Locator;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.List;

/**
 * Author: 345
 * Date: 24.09.13 7:23
 */
public class MyControls {

    public static final Dimension BUTTON_SIZE = new Dimension(190, 25);
    private static final Font BUTTON_FONT = new Font("Times New Roman", Font.BOLD, 10);
    private static final Font LABEL_FONT = new Font("Times New Roman", Font.BOLD, 15);
    private static Object[] SHIP_PARAMETRES = {"Название", "Профиль", "Водоизмещение", "Автономность", "Экипаж"};

    public static JButton createButton(String text, int x, int y) {
        JButton myButton = new JButton(text);
        myButton.setSize(BUTTON_SIZE);
        myButton.setLocation(x, y);
        myButton.setFont(LABEL_FONT);
        return myButton;
    }

    public static JLabel createLabel(String text, int x, int y) {
        JLabel myLabel = new JLabel(text);
        myLabel.setSize(BUTTON_SIZE);
        myLabel.setFont(LABEL_FONT);
        myLabel.setLocation(x, y);
        return myLabel;
    }

    public static JTextArea CreateJTextArea(int x, int y, int width, int height, boolean readOnly) {
        JTextArea myJTextArea = new JTextArea();
        myJTextArea.setFont(LABEL_FONT);
        myJTextArea.setSize(width, height);
        myJTextArea.setLocation(x, y);
        myJTextArea.setFocusable(!readOnly);

        return myJTextArea;
    }

    public static JTextField CreateJTextField(int x, int y, boolean readOnly) {
        JTextField myJTextField = new JTextField();
        myJTextField.setFont(LABEL_FONT);
        myJTextField.setSize(BUTTON_SIZE);
        myJTextField.setLocation(x, y);
        myJTextField.setFocusable(!readOnly);
        return myJTextField;
    }

    public static JPanel CreateJPanel(Frame frame, int x, int y, int width, int height, String text) {
        JPanel myJPanel = new JPanel();
        myJPanel.setLayout(null);
        myJPanel.setSize(width, height);
        myJPanel.setLocation(x, y);
        myJPanel.setFont(LABEL_FONT);
        myJPanel.setBorder(BorderFactory.createTitledBorder(text));
        frame.add(myJPanel);
        return myJPanel;
    }

    public static File OpenFileDialog(JFrame frame) {
        JFileChooser myJFileChooser = new JFileChooser();
        myJFileChooser.showDialog(frame, "Открыть файл");

        return myJFileChooser.getSelectedFile();
    }

    public static DefaultListModel CreateJList(JFrame frame, int x, int y, int width, int height) {
        DefaultListModel<String> listModel = new DefaultListModel<>();
        JList<String> list = new JList<>(listModel);
        list.setLocation(x, y);
        list.setSize(width, height);
        list.setFont(LABEL_FONT);
        list.setLayoutOrientation(JList.VERTICAL);
        frame.add(list);

        return listModel;
    }

    public static DefaultListModel CreateJList(JPanel jPanel, int x, int y, int width, int height) {
        DefaultListModel<String> listModel = new DefaultListModel<>();
        JList<String> list = new JList<>(listModel);
        list.setLocation(x, y);
        list.setSize(width, height);
        list.setFont(LABEL_FONT);
        list.setLayoutOrientation(JList.VERTICAL);

        jPanel.add(list);

        return listModel;
    }

    public static JTable fillExpertTable(int x, int y, int width, int height) {
        JTable table;
        List<Ship> shipList = Locator.getDatabase().getShips();
        String[][] data = new String[shipList.size()][5];
        for (int i = 0; i < shipList.size(); i++) {
            data[i][0] = shipList.get(i).getName();
            data[i][1] = shipList.get(i).getShipProfile().getName();
            data[i][2] = String.valueOf(shipList.get(i).getWaterVolume());
            data[i][3] = String.valueOf(shipList.get(i).getMaxDistance());
            data[i][4] = String.valueOf(shipList.get(i).getMaxStaff());
        }
        table = new JTable((Object[][]) data, (Object[]) SHIP_PARAMETRES);
        table.setLocation(x, y);
        table.setSize(width, height);
        return table;
    }
}
